// 111.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;

#define MAX_LENGTH 100

template< typename T, typename _InIt, typename _Fn1 >
void LIS( _InIt begin, _InIt end, _Fn1 f )
{
	struct LISN
	{
		int prev;
		int length;

		LISN()
		{
			prev = -1;
			length = 1;
		}
	};

	LISN nodes[ MAX_LENGTH ];

	int maxpos = -1;

	for( int i = 1; i < end - begin; i++ )
	{
		int pos = -1;
		for( int k = 0; k < i; k++ )
		{
			if( *(begin + k) < *(begin + i) )
			{
				if( pos == -1 || nodes[pos].length < nodes[k].length )
				{
					pos = k;
				}
			}
		}

		if( pos != -1 )
		{
			nodes[i].prev = pos;
			nodes[i].length = nodes[pos].length + 1;
		}

		if( maxpos == -1 || nodes[maxpos].length < nodes[i].length )
		{
			maxpos = i;
		}
	}

	T* rlist[ MAX_LENGTH ] = { NULL };

	int j = 0;
	for( int i = maxpos; i != -1; i = nodes[i].prev )
	{
		rlist[j++] = &( *(begin + i) );
	}

	f( rlist, j );
}

void print_ans( int** list, int count )
{
	cout << count << endl;
}

int main(int argc, char* argv[])
{
	int dim = 0;
	cin >> dim;

	int cor[20] = {0};
	for( int i = 0; i < dim; i++ )
	{
		cin >> cor[i];
	}

	while( 1 )
	{
		int req[20] = {0};
		int dyn[20] = {0};
		for( int i = 0; i < dim; i++ )
		{
			if( !( cin >> req[i] ) )
			{ return 0; }

			dyn[req[i] - 1] = cor[i];
		}
		LIS<int>( dyn, dyn + dim, print_ans );
	}

	return 0;
}

